package svc

import (
	"time"

	"diff.wang/asynqsrv/crond/internal/config"
	"github.com/hibiken/asynq"
)

type ServiceContext struct {
	Config              config.Config
	PeriodicTaskManager *asynq.PeriodicTaskManager
}

func NewServiceContext(c config.Config) *ServiceContext {

	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
	mgr, err := asynq.NewPeriodicTaskManager(
		asynq.PeriodicTaskManagerOpts{
			RedisConnOpt:               asynq.RedisClientOpt{Addr: c.Redis.Host, DB: c.RedisDB},
			PeriodicTaskConfigProvider: NewFileBaseConfigProvider(c.JobFilename), // this provider object is the interface to your config source
			SyncInterval:               10 * time.Second,                         // this field specifies how often sync should happen
			SchedulerOpts:              &asynq.SchedulerOpts{Location: loc},
		})
	if err != nil {
		panic(err)
	}
	return &ServiceContext{
		Config:              c,
		PeriodicTaskManager: mgr,
	}
}
